#import "LoginRequest.h"
#import "Utils.h"
#import "SBJson.h"
#import "iShopAppDelegate.h"
#import "Category.h"
#import "Brand.h"
#import "CoreDataUtils.h"
#import "WishList.h"

@implementation LoginRequest

-(void) makeRequest:(NSDictionary*)dictionary
{
	[Utils setValue:[NSNull null] forRefKey:kSessionKey];
	[Utils setValue:[dictionary valueForKey:kPassword] forRefKey:KPassword];
	NSLog(@"%@",[dictionary JSONRepresentation]);
	[super requestTo:@"Login" withBody:[dictionary JSONRepresentation]];
}

-(void) notifyIfConnectionFail:(NSError*)error
{
	BOOL rsRet = NO;
	if (_delegate && [_delegate respondsToSelector:@selector(processRequestResult:)]) {
		[_delegate processRequestResult:rsRet];
	}
	NSLog(@"notifyIfConnectionFail:%@",[error localizedDescription]);
	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Login Fail" message:@"Connection Error" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

-(void) processResult:(NSDictionary*)result
{
	//{"LoginResult":{"ErrorMessage":"SUCCESSFUL","isSuccessful":true,"SessionKey":"5d8c379f-a44e-4704-a7f3-b839a110dde5"}}
	NSDictionary *resultDict = [result valueForKey:@"LoginResult"];
	if (resultDict) {
		if ([[resultDict objectForKey:@"isSuccessful"] boolValue] == YES) {
			[Utils setValue:[resultDict objectForKey:@"SessionKey"] forRefKey:kSessionKey];
			if ([[resultDict objectForKey:@"CustomerCategories"] isKindOfClass:[NSArray class]]) {
				for (NSDictionary *dic in [resultDict objectForKey:@"CustomerCategories"]) {
					NSDictionary *condition= [[NSMutableDictionary alloc] init];
					[condition setValue:[dic objectForKey:@"CategoryID"] forKey:@"CategoryID"];
					Category *catObj = (Category*)[CoreDataUtils getObjectEntity:@"Category" fromCondition:condition];
					if (catObj) {
						catObj.CustomerCategoryOrder = [dic objectForKey:@"CategoryOrder"];
					}
				}
			}
			if ([[resultDict objectForKey:@"CustomerBrands"] isKindOfClass:[NSArray class]]) {
				for (NSDictionary *dic in [resultDict objectForKey:@"CustomerBrands"]) {
					NSDictionary *condition= [[NSMutableDictionary alloc] init];
					[condition setValue:[dic objectForKey:@"BrandID"] forKey:@"BrandID"];
					Brand *brandObj = (Brand*)[CoreDataUtils getObjectEntity:@"Brand" fromCondition:condition];
					if (brandObj) {
						brandObj.BrandCheck = [dic objectForKey:@"BrandSelected"];
					}
				}
			}
			if ([[resultDict objectForKey:@"CustomerWishlists"] isKindOfClass:[NSArray class]]) {
				[CoreDataUtils deleteAllWishlist];
				for (NSDictionary *dic in [resultDict objectForKey:@"CustomerWishlists"]) {
					[CoreDataUtils addWishList:[dic objectForKey:@"BaseProductID"]];
				}
			}
			[((iShopAppDelegate*)[[UIApplication sharedApplication] delegate]) saveContext];						   
		}
		else {
			//NSLog(@"notifyIfConnectionFail:%@",[error localizedDescription]);
			UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Login Fail" message:[resultDict objectForKey:@"ErrorMessage"] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}

	}
	BOOL rsRet = [[resultDict objectForKey:@"isSuccessful"] boolValue];
	if (_delegate && [_delegate respondsToSelector:@selector(processRequestResult:)]) {
		[_delegate processRequestResult:rsRet];
	}
}

@end
